package kr.co.skplanet.commute.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class StringUtils {
	
	public static String getRegdate(int year, int month, int day) {
		
		StringBuffer sb = new StringBuffer();
		sb.append(year);
		
		if(month < 10){
			sb.append("0");
			sb.append(month);
		}else{
			sb.append(month);
		}
		if(day < 10){
			sb.append("0");
			sb.append(day);
		}else{
			sb.append(day);
		}
		return sb.toString();
	}
	
	public static String getRegtime(int hour, int minute, int second){
		StringBuffer sb = new StringBuffer();

		if(hour < 10){
			sb.append("0");
			sb.append(hour);
		}else{
			sb.append(hour);
		}
		if(minute < 10){
			sb.append("0");
			sb.append(minute);
		}else{
			sb.append(minute);
		}
		if(second < 10){
			sb.append("0");
			sb.append(second);
		}else{
			sb.append(second);
		}

		return sb.toString();
	}	
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	public static boolean hasLength(CharSequence str) {
		return (str != null && str.length() > 0);
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	public static boolean hasLength(String str) {
		return hasLength((CharSequence) str);
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	public static boolean hasText(CharSequence str) {
		if (!hasLength(str)) {
			return false;
		}
		int strLen = str.length();
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * @param str
	 * @return
	 */
	public static boolean hasText(String str) {
		return hasText((CharSequence) str);
	}
	
	/**
	 * 
	 * @param length
	 * @return
	 */
	public static String genRandomString( int length )
	{
		final char[] possible = {'0','1','2','3','4','5','6','7','8','9','0', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
		
		StringBuffer rString = new StringBuffer( "" );
		int i = 0;
		int lenPossible = possible.length;
		
		Random rn = new Random();
		
		while( i < length ) {
            int pos = rn.nextInt() % lenPossible;
            if ( pos < 0 ) { pos = -pos; }
            if( rString.toString().indexOf( possible[ pos ] ) == -1 ) { 
            	rString.append( possible[ pos ] );
				i++;
    		}
    	}
		
		return rString.toString();
	}
	
	public static String genRandomStringWithSpecialCharacter( int length )
	{
		final char[] possible = {'0','1','2','3','4','5','6','7','8','9','0', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',  '!', '@', '#', '$', '%', '^', '&', '*', '(', ')'};
		
		StringBuffer rString = new StringBuffer( "" );
		int i = 0;
		int lenPossible = possible.length;
		
		Random rn = new Random();
		
		while( i < length ) {
            int pos = rn.nextInt() % lenPossible;
            if ( pos < 0 ) { pos = -pos; }
            if( rString.toString().indexOf( possible[ pos ] ) == -1 ) { 
            	rString.append( possible[ pos ] );
				i++;
    		}
    	}
		
		return rString.toString();
	}
	
	public static String genMD5(String str){
    	String MD5 = ""; 
    	try{
    		MessageDigest md = MessageDigest.getInstance("MD5"); 
    		md.update(str.getBytes()); 
    		byte byteData[] = md.digest();
    		StringBuffer sb = new StringBuffer(); 
    		for(int i = 0 ; i < byteData.length ; i++){
    			sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
    		}
    		MD5 = sb.toString();
    		
    	}catch(NoSuchAlgorithmException e){
    		e.printStackTrace(); 
    		MD5 = null; 
    	}
    	return MD5;
    }
	
	public static String genSHA256(String str){
    	String SHA256 = ""; 
    	try{
    		MessageDigest md = MessageDigest.getInstance("SHA-256"); 
    		md.update(str.getBytes()); 
    		byte byteData[] = md.digest();
    		StringBuffer sb = new StringBuffer(); 
    		for(int i = 0 ; i < byteData.length ; i++){
    			sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
    		}
    		SHA256 = sb.toString();
    		
    	}catch(NoSuchAlgorithmException e){
    		e.printStackTrace(); 
    		SHA256 = null; 
    	}
    	return SHA256;
    }
	
	public static String genSHA256(String str, String salt, int iteration){
    	String SHA256 = "";
    	String strToHash = str + salt;
    	try{
    		MessageDigest md = MessageDigest.getInstance("SHA-256"); 
    		md.update(strToHash.getBytes()); 
    		byte byteData[] = md.digest();
    		
    		// Iteration
    		for(int i = 0; i < iteration; i++) {
    			md.reset();
    			byteData = md.digest(byteData);
    		}
    		
    		StringBuffer sb = new StringBuffer(); 
    		for(int i = 0 ; i < byteData.length ; i++){
    			sb.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
    		}
    		SHA256 = sb.toString();
    		
    	} catch (NoSuchAlgorithmException e){
    		e.printStackTrace(); 
    		SHA256 = null; 
    	}
    	return SHA256;
    }
}